<!DOCTYPE html>
<html>
<head>
    <title>攻略系列</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/checkLogin.js"></script>
    <script>
        $(function () {
            //当前攻略的id
            var params = getParams();
            //当前登录用户的id
            var userId=user.id;
            //增加当前攻略阅读量
            $.post(baseUrl+'/strategies/'+params.id+'/readCount',function (data) {
                //回显阅读数
                var readCount=parseInt($('#readCount').html())
                $('#readCount').html(readCount+1)
            })
            //获取当前旅行单的加入状态
            $.get(baseUrl + '/strategies/checkTravelList',{userId:userId,strategyId:params.id}, function (data) {
                if (data.success) {
                    //已加入旅行单
                    $('#travelListBtn').html('已加入旅行单')
                    //设置已加入状态
                    $('#travelListBtn').attr('data-type',1)
                }else{
                    //未收藏
                    $('#travelListBtn').html('加入旅行单')
                    //设置未收藏状态
                    $('#travelListBtn').attr('data-type',0)
                }
            });
            //加入旅行单,移除旅行单
            $('#travelListBtn').click(function () {
                //查看当前旅行单状态
                var type=$(this).data('type')
                if (type){
                    //发送移除旅行单请求
                    $.ajax({
                        url: baseUrl + "/strategies/travelList"+'?userId='+userId+'&strategyId='+params.id,
                        method: "delete",
                        success: function (data) {
                            //已加入--->未加入
                            $('#travelListBtn').html('加入旅行单')
                            $('#travelListBtn').data('type',0);
                           //弹框
                            $(document).dialog({
                                type: 'confirm',
                                content: '移除旅行单成功!',
                            });
                        }
                    })
                }else{
                    //发送加入旅行单请求
                    $.ajax({
                        url: baseUrl + "/strategies/travelList"+'?userId='+userId+'&strategyId='+params.id,
                        method: "put",
                        success: function () {
                            //未收藏---->已收藏
                            $('#travelListBtn').html('已加入旅行单')
                            $('#travelListBtn').data('type',1);
                            //弹框
                            $(document).dialog({
                                type: 'confirm',
                                content: '加入旅行单成功!',
                            });
                        }
                    })
                }
            })
            //获取当前攻略的收藏状态
            $.get(baseUrl + '/strategies/checkFavorite',{userId:userId,strategyId:params.id}, function (data) {
                if (data.success) {
                    //已收藏
                    $('#favorite').html('已收藏')
                    $('#favorite').attr('class','fa fa-heart')
                    //设置已收藏状态
                    $('#favorite').attr('data-type',1)
                }else{
                    //未收藏
                    $('#favorite').html('收藏')
                    $('#favorite').attr('class','fa fa-heart-o')
                    //设置未收藏状态
                    $('#favorite').attr('data-type',0)
                }
            });
            //点击收藏,取消收藏
            $('#favorite').click(function () {
                //查看当前收藏状态
                var type=$(this).data('type')
                if (type){
                    //发送取消收藏请求
                    $.ajax({
                        url: baseUrl + "/strategies/myFavorite"+'?userId='+userId+'&strategyId='+params.id,
                        method: "delete",
                        success: function (data) {
                            //已收藏--->未收藏
                            $('#favorite').html('收藏')
                            $('#favorite').attr('class','fa fa-heart-o')
                            $('#favorite').data('type',0);
                            //回显收藏数
                            var favoriteCount=parseInt($('#favoriteCount').html())
                            $('#favoriteCount').html(favoriteCount-1)
                        }
                    })
                }else{
                    //发送收藏请求
                    $.ajax({
                        url: baseUrl + "/strategies/myFavorite"+'?userId='+userId+'&strategyId='+params.id,
                        method: "put",
                        success: function () {
                            //未收藏---->已收藏
                            $('#favorite').html('已收藏')
                            $('#favorite').attr('class','fa fa-heart')
                            $('#favorite').data('type',1);
                            //回显收藏数
                            var favoriteCount=parseInt($('#favoriteCount').html())
                            $('#favoriteCount').html(favoriteCount+1)
                        }
                    })
                }
            })
            // 根据攻略id查询攻略信息
            $.get(baseUrl + '/strategies/' + params.id, function (data) {
                $(".strategies").renderValues(data);
            });
            // 根据攻略id查询攻略分类信息
            $.get(baseUrl + '/strategies/' + params.id + "/catalogs", function (data) {
                var index = 1;
                $(".catalogs").renderValues({list: data}, {
                    getHref: getHref,
                    getName: function (item, value) {
                        $(item).html(index++ + "/" + value);
                    }
                });
            });
            // 点击添加评论跳转至编辑评论界面
            $("#commentBtn").click(function () {
                window.location.href = "/mine/addComment.html?id=" + params.id;
            });

            var currentPage = 1;    // 默认第一页
            var pages;  // 总页数
            var travelArr = []; // 用户存储游记文章的容器
            var loading = false;    // 标记是否查询中

            function query() {  // 分页查询
                if (loading) {    // 代表正在查询,就直接结束方法
                    return;
                }
                loading = true; // 标记正在查询中;
                // 根据攻略id查询评论信息
                $.get(baseUrl + "/strategies/" + params.id + "/comments", {currentPage: currentPage,strategyId:params.id}, function (data) {
                    $.merge(travelArr, data.list);  // 合并数据,把第二个参数的数据合并到第一个参数的容器中
                    var json = {list: travelArr};    // 用于render-loop="list"
                    $("#comment").renderValues(json, {
                        getHref: getHref,
                        getStar: function (item, value) {
                            var temp = '';
                            for (var i = 0; i < value; i++) {
                                temp += '<i class="fa fa-star"></i>';
                            }
                            for (var i = 0; i < 5 - value; i++) {
                                temp += '<i class="fa fa-star-o"></i>';
                            }
                            $(item).html(temp);
                        },
                        getThumbState:function (item, value) {
                            //评论的id
                            var commentId=value;
                            //绑定按钮的评论id
                            $(item).attr('data-id',commentId);
                            //根据评论的id查询是否已点赞
                            $.ajax({
                                url: baseUrl + '/strategyComments/thumbupState',
                                async: false,
                                method: "get",
                                data:{commentId: commentId, userId: userId},
                                success: function (data) {
                                    if (data.success) {
                                        //已点赞
                                        $(item).html('已赞')
                                        $(item).css("color","blue")
                                        $(item).attr('class','fa fa-thumbs-up thumbBtn')
                                        //设置已点赞状态
                                        $(item).attr('data-type',1)
                                    }else{
                                        //未点赞
                                        $(item).html('赞一下')
                                        $(item).css("color","red")
                                        $(item).attr('class','fa fa-thumbs-o-up thumbBtn')
                                        //设置未点赞状态
                                        $(item).attr('data-type',0)
                                    }
                                }
                            });

                        }

                    });

                    //点击点赞,取消点赞
                    $('.thumbBtn').click(function () {
                        //查看当前点赞状态
                        var type=$(this).data('type');
                        var commentId=$(this).data('id')
                        var thumbButton=$(this)
                        if (type==1){
                            console.log(type);
                            //发送取消点赞请求
                            $.ajax({
                                url: baseUrl + "/strategyComments/thumbup"+'?commentId='+commentId+'&currentUserId='+userId,
                                method: "delete",
                                success: function (data) {
                                    //已点赞--->未点赞
                                    thumbButton.html('赞一下')
                                    thumbButton.data('type',0)
                                    thumbButton.css("color","red")
                                    //获得点赞数
                                    var count=parseInt(thumbButton.next().html()) ;
                                    console.log(count);
                                    thumbButton.next().html(count-1)
                                }
                            })
                        }else{
                            //发送点赞请求
                            console.log(type);
                            $.ajax({
                                url: baseUrl + "/strategyComments/thumbup"+'?commentId='+commentId+'&currentUserId='+userId,
                                method: "put",
                                success: function () {
                                    //未点赞---->已点赞
                                    thumbButton.html('已赞')
                                    thumbButton.data('type',1)
                                    thumbButton.css("color","blue")
                                    //获得点赞数
                                    var count=parseInt(thumbButton.next().html()) ;
                                    console.log(count);
                                    thumbButton.next().html(count+1)

                                }
                            })
                        }
                    })
                    // 绑定图片的点击事情
                    $(".comment-img img").click(function () {
                        // 获取点击的图片
                        var src = $(this).attr('src');
                        $(document).dialog({
                            style: 'ios',
                            titleShow: false,
                            overlayClose: true,
                            content: '<img src="' + src + '" width="100%"/>'
                        });
                    });
                    currentPage++;  // 当前页数+1
                    pages = data.pages; // 设置总页数
                    loading = false;    // 标记查询结束

                });

            }

            // 默认查询第一页
            query();
            // 监听窗口的滚动事件
            $(window).scroll(function () {
                console.log("窗口滚动中...")
                if ($(document).scrollTop() + $(window).height() >= $(document).height()) { // 滚动到底部
                    // 如果已经查询到最后一页,就不再查
                    if (currentPage <= pages) {
                        query();
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            content: '<span class="info-text">已经到底了!</span>',
                            autoClose: 1500
                        });
                    }
                }
            });
        });
    </script>
</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="搜索旅行攻略">
            </div>
        </div>
    </div>
</div>

<div class="container strategies">
    <h5 class="title" render-html="title">广州攻略</h5>
    <small><span id="readCount" render-html="readCount" >0</span>人阅读过此攻略</small>
    <div class="focus">
        <i id="favorite" class="fa fa-heart" data-type=""> 收藏</i><span id="favoriteCount" render-html="favoriteCount">0</span>
        <!--<i class="fa fa-thumbs-o-up" style="color: deepskyblue"> 点赞</i><span id="thumbupCount" render-html="thumbupCount"> 0</span>-->
        <button class="btn">想去</button>
        <button class="btn">去过</button>
    </div>
</div>
<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab">
    <li class="nav-item ">
        <a class="nav-link active" id="pills-catalog-tab" data-toggle="pill" href="#pills-catalog">
            <span>攻略目录</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link " id="pills-comment-tab" data-toggle="pill" href="#pills-comment">
            <span>骡友点评</span>
        </a>
    </li>
</ul>
<div class="tab-content " id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-catalog">

        <div class="catalogs">
            <div render-loop="list">
                <div class="container detail">
                    <h6 render-key="list.name" render-fun="getName"></h6>
                    <div render-loop="list.details">
                        <a data-url="strategyDetails.html?id=" render-key="list.details.id" render-fun="getHref">
                            <button class="btn" render-html="list.details.title"></button>
                        </a>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="tab-pane fade " id="pills-comment">

        <div class="container tag">
            <h6>骡友标签</h6>
            <div>
                <span>价格便宜</span>
                <span>很热闹</span>
                <span>交通方便</span>
                <span>吃的很多</span>
                <span>风景优美</span>
                <span>美食很多</span>

            </div>
        </div>
        <hr>

        <div id="comment">
            <div render-loop="list">
                <div class="container row comment">
                    <div class="col-2 comment-head">
                        <a render-key="list.user.id" render-fun="getHref" data-url="/userProfiles.html?id=">
                            <img class="rounded-circle"
                                 render-src="list.user.headImgUrl">
                        </a>
                    </div>
                    <div class="col comment-right">
                        <p class="authorName"><span render-html="list.user.nickName"></span></p>
                        <span class="comment-star" render-key="list.starNum" render-fun="getStar">

                        </span>
                        <span class="comment-date" render-html="list.createTime">2018-07-19</span>
                        <div class="comment-content">
                            <a href="#">
                                <p render-html="list.content"></p>
                            </a>
                        </div>

                        <ul class="comment-img" render-loop="list.imgUrlsArr">
                            <li>
                                <img render-src="list.imgUrlsArr.self">
                            </li>
                        </ul>

                        <div class="d-flex icon">
                            <a render-key="list.id" render-fun="getHref" data-url="/strategyComment.html?id=">
                                <i class="fa  fa-commenting-o " style="margin-right: 10px"> 评论<span render-html="list.viewCount"></span> </i>
                            </a>
                            <a>
                            <i  class="fa fa-thumbs-o-up thumbBtn" style="color: black" data-type data-id render-key="list.id" render-fun="getThumbState"></i><span render-html="list.thumbupCount"></span>
                            </a>

                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>

</div>
</div>

<div class="operation">
    <button data-type="" id="travelListBtn" class="btn">加入旅行单</button>
    <button class="btn" id="commentBtn">点评</button>
</div>

</body>
</html>